package com.tongzhou.tongzhou.update.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.update.dao.IUpdateCodeDAO;
import com.tongzhou.tongzhou.update.model.SearchUpdateCodeCondition;
import com.tongzhou.tongzhou.update.model.UpdateCode;
import com.tongzhou.util.FileUtil;

public class UpdateCodeAction extends ActionSupport{
	private int id;
	private UpdateCode updateCode;
	private List<UpdateCode> result=null;
	private SearchUpdateCodeCondition condition=null;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	
	private IUpdateCodeDAO dao=null;
	
	private File appUrl ;
	private String appUrlFileName ;
	/**
	 * 版本信息列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listUpdateCode() throws DBException{
		checkSearchCondition();
		condition = new SearchUpdateCodeCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
	    result = dao.listUpdateCode(condition);
		return "listUpdateCode";
	} 
	
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	
	/**
	 * 新增版本信息
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddUpdateCode() throws DBException{
		return "addUpdateCode";
	}
	
	public String addUpdateCode() throws DBException{
		if(appUrl!=null){
			String path = "updateCode"+File.separator+appUrlFileName;
			path=path.replace("\\","\\\\");
			FileUtil.copyFile(appUrl, new File(FileUtil.getWebRootDir()+path));
			updateCode.setAppUrl(path);
		}
		dao.addUpdateCode(updateCode);
		listUpdateCode();
		return "listUpdateCode";
	}
	
	public String deleteUpdateCode() throws DBException{
//		dao=new UserMgrDAO();
		dao.deleteUpdateCode(id);
		listUpdateCode();
		return "listUpdateCode";
	}
	
	public String toEditUpdateCode() throws DBException{
		updateCode=dao.getUpdateCodeById(id);
		return "editUpdateCode";
	}
	
	public String editUpdateCode() throws DBException{
		if(appUrl!=null){
			String path = "updateCode"+File.separator+appUrlFileName;
			path=path.replace("\\","\\\\");
			FileUtil.copyFile(appUrl, new File(FileUtil.getWebRootDir()+path));
			updateCode.setAppUrl(path);
		}else{
			updateCode.setAppUrl(updateCode.getAppUrl().replace("\\","\\\\"));
		}
		dao.updateUpdateCode(updateCode);
		listUpdateCode();
		return "listUpdateCode";
	}
	
	/**
	 * 下载文件
	 */
	public String downLoadFile(){
		HttpServletResponse response = ServletActionContext.getResponse();
		
//		PrintWriter out = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			updateCode=dao.getUpdateCodeById(id);
			String filedisplay ="";
			int len=0;
			if(updateCode.getAppUrl().contains("\\")){
				len=updateCode.getAppUrl().replace("\\", "`").split("`").length;
				filedisplay =updateCode.getAppUrl().replace("\\", "`").split("`")[len-1]; 
			}else{
				len=updateCode.getAppUrl().replace("/", "`").split("`").length;
				filedisplay =updateCode.getAppUrl().replace("/", "`").split("`")[len-1]; 
			}
			response.setContentType("application/x-msdownload;");
			response.setHeader("Content-disposition", "attachment; filename="
			  + new String(filedisplay.getBytes("gb2312"), "ISO8859-1"));
			response.setHeader("Content-Length", String.valueOf(filedisplay));
			response.setCharacterEncoding("utf-8");
			bis = new BufferedInputStream(new FileInputStream(updateCode.getAppUrl()));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
	  		if (bis != null){
	  			try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	  		}
	  		if (bos != null){
	  			try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	  		}
	  	}
		return null;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public UpdateCode getUpdateCode() {
		return updateCode;
	}


	public void setUpdateCode(UpdateCode updateCode) {
		this.updateCode = updateCode;
	}


	public List<UpdateCode> getResult() {
		return result;
	}


	public void setResult(List<UpdateCode> result) {
		this.result = result;
	}


	public SearchUpdateCodeCondition getCondition() {
		return condition;
	}


	public void setCondition(SearchUpdateCodeCondition condition) {
		this.condition = condition;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public String getOrderBy() {
		return orderBy;
	}


	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}


	public String getOrderByType() {
		return orderByType;
	}


	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}


	public IUpdateCodeDAO getDao() {
		return dao;
	}


	public void setDao(IUpdateCodeDAO dao) {
		this.dao = dao;
	}


	public File getAppUrl() {
		return appUrl;
	}


	public void setAppUrl(File appUrl) {
		this.appUrl = appUrl;
	}


	public String getAppUrlFileName() {
		return appUrlFileName;
	}


	public void setAppUrlFileName(String appUrlFileName) {
		this.appUrlFileName = appUrlFileName;
	}
}
